package com.zb.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zb.constant.WorkConstant;
import com.zb.entity.ZjWorkChangeEngineer;
import com.zb.mapper.ZjWorkChangeEngineerMapper;
import com.zb.service.ZjWorkChangeEngineerService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 网点工单维修师傅改派表 服务实现类
 * </p>
 *
 * @author WEN
 * @since 2023-12-05
 */
@Service
public class ZjWorkChangeEngineerServiceImpl extends ServiceImpl<ZjWorkChangeEngineerMapper, ZjWorkChangeEngineer> implements ZjWorkChangeEngineerService {
    @Resource
    private ZjWorkChangeEngineerMapper workChangeEngineerMapper;


    // 根据服务商ID & 工单ID获取转单待审核的工单
    @Override
    public List<ZjWorkChangeEngineer> getTransferOrderStatutWaitCheckStatus(Long networkId, List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkChangeEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("work_id", "apply_network_emp", "gmt_create");
        wrapper.eq("network_id", networkId)
                .eq("status", WorkConstant.WORK_CHANGE_ENGINEER_STATUS_1)
                .in("work_id", workIds);
        return this.list(wrapper);
    }

    // 根据[服务商ID]&[工单ID]获取转单待审核的申请单
    @Override
    public ZjWorkChangeEngineer getWaitCheckTransferOrderApply(Long networkId, Long workId) {
        QueryWrapper<ZjWorkChangeEngineer> wrapper = new QueryWrapper<>();
        wrapper.eq("status", WorkConstant.WORK_CHANGE_ENGINEER_STATUS_1)
                .eq("network_id", networkId)
                .eq("work_id", workId)
                .last("LIMIT 1");
        return this.getOne(wrapper);
    }

    // 根据工单ID查询是否存在申请转单待审核的
    @Override
    public boolean isApplyChangeEngineerWaitCheck(Long workId) {
        QueryWrapper<ZjWorkChangeEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("id", "status")
                .eq("status", WorkConstant.WORK_CHANGE_ENGINEER_STATUS_1)
                .eq("work_id", workId);
        ZjWorkChangeEngineer engineer = this.getOne(wrapper);
        return null != engineer && engineer.getStatus() == WorkConstant.WORK_CHANGE_ENGINEER_STATUS_1;
    }

    // 根据服务人员ID & 工单ID获取已申请取消工单的数据
    @Override
    public List<ZjWorkChangeEngineer> getWaitCheckTransferOrderApplyByNetworkEmpIdAndWorkIds(Long networkEmpId, List<Long> workIds) {
        if (CollectionUtil.isEmpty(workIds)) {
            return new ArrayList<>();
        }
        QueryWrapper<ZjWorkChangeEngineer> wrapper = new QueryWrapper<>();
        wrapper.select("id", "work_id", "status")
                .eq("status", WorkConstant.WORK_CHANGE_ENGINEER_STATUS_1)
                .eq("apply_network_emp_id", networkEmpId)
                .in("work_id", workIds);
        return this.list(wrapper);
    }

}
